package com.hfjyz.exam.validator;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import com.hfjyz.exam.command.ModifyPasswordCommand;
import com.hfjyz.exam.util.ValidatorUtil;

public class ModifyPasswordValidator implements Validator {

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public boolean supports(Class clazz) {
		return clazz.isAssignableFrom(ModifyPasswordCommand.class);
	}

	public void validate(Object target, Errors errors) {
		validateRegistInfo(target, errors);
	}

	public void validateRegistInfo(Object target, Errors errors) {
		ModifyPasswordCommand v = (ModifyPasswordCommand) target;

		if (ValidatorUtil.isEmpty(v.getOldPassword())) {
			errors.rejectValue("oldPassword",
					"error.modifypassword.oldpassword.required", "旧密码必须填写!");
		}
		if (ValidatorUtil.isEmpty(v.getPassword())) {
			errors.rejectValue("password",
					"error.modifypassword.password.required", "新密码必须填写!");
		} else {
			if (!v.getPassword().equals(v.getRePassword())) {
				errors.rejectValue("rePassword",
						"error.modifypassword.repassword.invalid",
						"重复新密码与新密码必须一致!");
			}
		}
	}

}
